#include <string.h>
#include <stdio.h>
#include "mhscpu.h"

uint8_t tmpRece = 0;
uint32_t flagRece = 0;

void NVIC_Configuration(void);
void UART_Configuration(void);
uint8_t u8GetDate[1024] = {0};
uint32_t u32GetDateIndex = 0;
uint32_t u32GetDateNum = 256;

UART_TypeDef *UARTx = UART0;

int main(void)
{
	SYSCTRL_APBPeriphClockCmd(SYSCTRL_APBPeriph_UART0 | SYSCTRL_APBPeriph_UART1 |  SYSCTRL_APBPeriph_GPIO,ENABLE);
	SYSCTRL_APBPeriphResetCmd(SYSCTRL_APBPeriph_UART0 | SYSCTRL_APBPeriph_UART1 , ENABLE);
	
	GPIO_PinRemapConfig(GPIOA, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3, GPIO_Remap_0);
	GPIO_PinRemapConfig(GPIOB, GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15, GPIO_Remap_3);
	
	NVIC_Configuration();
	UART_Configuration();
	
	printf("MegaHunt SCPU UART Demo V1.0.\n");
	
	while(1) 
	{
		u32GetDateIndex = 0;
		if(u32GetDateIndex < u32GetDateNum)
			UART_SetRTS(UARTx);
		do{
			if(UART_GetLineStatus(UARTx) & UART_LINE_STATUS_RX_RECVD)
			{
				u8GetDate[u32GetDateIndex] = UART_ReceiveData(UARTx);
				u32GetDateIndex ++;
			}
		}while(u32GetDateIndex < u32GetDateNum);
		UART_ResetRTS(UARTx);
		
		u32GetDateIndex = 0;
		while((u32GetDateIndex < u32GetDateNum))
		{
				while(!UART_IsTXEmpty(UARTx));
				UART_SendData(UARTx, u8GetDate[u32GetDateIndex]);
				u8GetDate[u32GetDateIndex] = 0;
				u32GetDateIndex++;
		}
	}
}

void NVIC_Configuration(void)
{
	NVIC_InitTypeDef NVIC_InitStructure;
	
	NVIC_SetPriorityGrouping(NVIC_PriorityGroup_0);

	NVIC_InitStructure.NVIC_IRQChannel = UART0_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

	NVIC_InitStructure.NVIC_IRQChannel = UART1_IRQn;
	NVIC_Init(&NVIC_InitStructure);
}

void UART_Configuration(void)
{
	UART_InitTypeDef UART_InitStructure;
	UART_FIFOInitTypeDef UART_FIFOInitStruct;

	UART_StructInit(&UART_InitStructure);
	UART_FIFOStructInit(&UART_FIFOInitStruct);
	
	UART_InitStructure.UART_BaudRate = 115200;
	UART_InitStructure.UART_WordLength = UART_WordLength_8b;
	UART_InitStructure.UART_StopBits = UART_StopBits_1;
	UART_InitStructure.UART_Parity = UART_Parity_No;
	
	UART_FIFOInitStruct.FIFO_Enable = ENABLE;
	UART_FIFOInitStruct.FIFO_DMA_Mode = UART_FIFO_DMA_Mode_0;
	UART_FIFOInitStruct.FIFO_RX_Trigger = UART_FIFO_RX_Trigger_1_4_Full;
	UART_FIFOInitStruct.FIFO_TX_Trigger = UART_FIFO_TX_Trigger_1_4_Full;
	UART_FIFOInitStruct.FIFO_TX_TriggerIntEnable = ENABLE;

	UART_Init(UART0, &UART_InitStructure);
	UART_Init(UART1, &UART_InitStructure);
	
	UART_FIFOInit(UART0, &UART_FIFOInitStruct);
	UART_AutoFlowCtrlCmd(UART0, ENABLE);
	UART_Init(UART0, &UART_InitStructure);
	
	UART_FIFOInit(UART1, &UART_FIFOInitStruct);
	UART_AutoFlowCtrlCmd(UART1, ENABLE);
	UART_Init(UART1, &UART_InitStructure);
}

int SER_PutChar (int ch) {

  while(!UART_IsTXEmpty(UARTx));
	UART_SendData(UARTx, (uint8_t) ch);
  return ch;
}

int fputc(int c, FILE *f)
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the  */
  if (c == '\n')  {
    SER_PutChar('\r');
  }
  return (SER_PutChar(c));
}



#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
	/* User can add his own implementation to report the file name and line number,
	 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

	/* Infinite loop */
	while (1)
	{
	}
}
#endif






